var path = require("path");
var fs = require("fs");
var class_template = require("./class_template");
const whitelist = ["header.html","footer.html"];

module.exports = async function(){
  let fileArr = await getFiles()
  fileArr.forEach(e => {
    if(whitelist.indexOf(e)==-1){
      let p = path.resolve(__dirname,"../template/",e);
      let v = path.resolve(__dirname,"../views/",e);
      fs.readFile(p,'utf8',async function(err,data){ 
        if(err){
          return false;
        }
        let h = await class_template(data); 
        fs.writeFile(v,h,function(err){
          console.log("写入成功")
        })
      })
    }
  });
}

function getFiles(){
  let p = path.resolve(__dirname,"../template");
  return new Promise((resolve, reject) => {
    fs.readdir(p,function(err,files){
      if(err){
        console.log("未找到文件夹",err)
        resolve([]);
      }
      console.log("files",files)
      resolve(files);
    })
  });
}